package Client.GUI;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import Client.logic.ClientIF;
import Server.DataBase.Lesson;
import Server.Message.MessageGetFile;
import Server.Message.MessageGetStudentLesson;
import Server.Message.MessageGetTask;
import Server.Message.MessageGetTaskId;
import Server.Message.MessageGetTaskIdReplay;
import Server.Message.MessageLessonReplay;

import javax.imageio.ImageIO;
import javax.sql.rowset.serial.SerialException;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class GetTaskPanel extends MyJPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JComboBox comboBoxLesson, comboBoxTask;
	private Lesson lesson;
	private int taskId;
	private JFileChooser chooser;
	private JButton btnChooseDir;
	private String dir;

	public GetTaskPanel(ClientIF client) {
		super(PanelType.GET_TASK, client);
		
		BufferedImage myPic;
		try {
			myPic = ImageIO.read(new File("images"+ File.separator+"get task.jpg"));
		
			JLabel lblAddClass = new JLabel(new ImageIcon(myPic), JLabel.CENTER);
			lblAddClass.setBounds(12, 36, 324, 99);
			add(lblAddClass);
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		setLayout(null);
		init();
	}
	
	public void init(){
		initLabels();
		initComboBox();
		initBtn();
		
		getClient().sendMsgToServer(new MessageGetStudentLesson(getClient().getUser().getIdUser()));
		MessageLessonReplay msg = (MessageLessonReplay) getClient().getMessageFromServer();
		
		comboBoxLesson.addItem("choose");
		
		for (Lesson lesson : msg.getArray()) comboBoxLesson.addItem(lesson);
		comboBoxLesson.addActionListener(new LessonListener());
	}

	public void initLabels(){
		JLabel lblLesson = new JLabel("Lesson:");
		lblLesson.setBounds(54, 171, 59, 14);
		add(lblLesson);
		
		JLabel lblTask = new JLabel("Task:");
		lblTask.setBounds(54, 226, 46, 14);
		add(lblTask);
	}
	
	public void initComboBox(){
		comboBoxLesson = new JComboBox();
		comboBoxLesson.setBounds(123, 168, 111, 20);
		add(comboBoxLesson);
		
		comboBoxTask = new JComboBox();
		comboBoxTask.setBounds(123, 223, 111, 20);
		comboBoxTask.setEnabled(false);
		add(comboBoxTask);
	}
	
	public void initBtn(){
		btnChooseDir = new JButton("Download");
		btnChooseDir.addActionListener(new FileChooser());
		btnChooseDir.setEnabled(false);
		btnChooseDir.setBounds(123, 271, 111, 23);
		add(btnChooseDir);
	}
	
	private class LessonListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			try{
				lesson = (Lesson) comboBoxLesson.getSelectedItem();	
			
				getClient().sendMsgToServer(new MessageGetTaskId(lesson));
				MessageGetTaskIdReplay msg = (MessageGetTaskIdReplay) getClient().getMessageFromServer();
			
				if (msg.getMaxid()!=0){
					comboBoxTask.removeAllItems();
					comboBoxTask.addItem("Choose");
					for (int i=1; i<msg.getMaxid(); i++) comboBoxTask.addItem(i);
					comboBoxTask.setEnabled(true);
					comboBoxTask.addActionListener(new TaskListener());
				} 
			}catch (java.lang.Throwable t){}
		}
	}
	
	private class TaskListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			try{
				taskId =  (Integer) comboBoxTask.getSelectedItem();
				btnChooseDir.setEnabled(true);
				comboBoxTask.setEnabled(false);
			} catch (java.lang.Throwable t){}
		}
	}
	
	private class FileChooser implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			chooser = new JFileChooser(); 
			chooser.setCurrentDirectory(new java.io.File("."));
			chooser.setDialogTitle("Choose Folder");
			chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		    //
		    // disable the "All files" option.
		    //
			chooser.setAcceptAllFileFilterUsed(false);
		    //    
		    if (chooser.showSaveDialog((Component) getClient()) == JFileChooser.APPROVE_OPTION){
		    	dir = chooser.getSelectedFile().getAbsolutePath();
		    	getClient().sendMsgToServer(new MessageGetTask(lesson.getIdLesson(), taskId));
			    MessageGetFile msg = (MessageGetFile) getClient().getMessageFromServer();  
			    try {
			    		// write the inputStream to a FileOutputStream
			    		OutputStream out = new FileOutputStream(new File(dir+File.separator+msg.getFile().getFileName()));
			    		out.write(msg.getFile().getBlob().getBytes(1, msg.getFile().getSize()));
			    		out.close();
			    		System.out.println("New file created!");
			    		JOptionPane.showMessageDialog((Component) getClient(), "Download complete");
			    		getClient().swapFromBack(new GetTaskPanel(getClient()));
			    	    } catch (IOException e1) {
			    		System.out.println(e1.getMessage());
			    	    } catch (SerialException e2) {
							e2.printStackTrace();
						}
			}
		}
	}
		 
	@Override
	public MyJPanel pushPanel() {
		return new GetTaskPanel(getClient());
	}
}
